<?php

namespace App\Admin\Controllers;

use App\Admin\Grid\NGrid;
use App\Admin\Tools\Task\TaskAllocationTool;
use App\Admin\Tools\Task\TaskCreateTool;
use App\Admin\Tools\Task\TaskDeleteTool;
use App\Admin\Tools\Task\TaskImportTool;
use App\Admin\Tools\Task\TaskProgressTool;
use App\Admin\Tools\Task\TaskTransferTool;
use App\Models\TaskModel;
use App\Models\TaskUserModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Grid\Tools;
use Illuminate\Support\Facades\Auth;

class TaskController extends AdminController
{
    public function title(): string
    {
        return '任务管理';
    }

    public function grid(): NGrid
    {
        $grid = new NGrid(new TaskModel);
        $grid->model()->leftJoin('admin_users', 'admin_users.id', 'creator_id')
            ->select([
                'task.*',
                'admin_users.name',
            ])->orderByDesc('task.id');
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('task_name', __('task.taskName'));
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->like('remark', __('l.remark'));
            });
        });

        if (!ISADMIN) {
            if (in_array(Auth::user()->roles->first()->slug, ['boss', 'finance'])) $grid->model()->where('task.main_id', MAINID);
            else $grid->model()->where('task.creator_id', USERID);
        }
        if (Auth::user()->roles->first()->slug == 'Administrator') $grid->column('id', 'ID')->sortable();
        $grid->column('task_name', __('task.taskName'))->display(function () {
            if ($this->total_task_num > 0) return "<a href='/admin/task/related/" . $this->id . "' style='color: black;'>$this->task_name($this->total_task_num)</a>";
            else return $this->task_name . '(' . $this->total_task_num . ')';
        });
        $grid->column('remark', __('l.remark'));
        $grid->column('name', __('task.creator'));
        $grid->column('created_at', __('l.createdAt'))->display(function ($date) {
            return date('Y-m-d H:i:s', strtotime($date));
        });

        $grid->column('last_allocation_at', '最后调配时间')->display(function () {
            $toModel = TaskModel::query()->where('from_id', $this->id)->orderByDesc('id')->first();
            if(is_null($toModel)) return null;
            else return date('Y-m-d H:i:s', strtotime($toModel->created_at));
        });
        $grid->column('last_follow_at', '最后分配时间')->display(function ($date) {
            $allocationModel = TaskUserModel::query()
                ->where('task_id', $this->id)->orderByDesc('id')->first();
            if(is_null($allocationModel)) return null;
            else return date('Y-m-d H:i:s', strtotime($allocationModel->created_at));
        });

        $grid->actions(function (Actions $action) {
            $action->disableEdit();
            $action->disableView();
            $action->disableDelete();

            $taskUser = TaskUserModel::query()
                ->leftJoin('admin_users', 'admin_users.id', 'task_user.follow_user_id')
                ->leftJoin('task', 'task.id', 'task_user.task_id')
                ->where('task_id', $action->row->id)->select('admin_users.name as follow_user_name', 'task_user.*')
                ->get()->toArray();

            if (in_array(USERID, [1, 2, 3]) && $action->row->total_task_num > 0) $action->append(new TaskTransferTool($action->row->id, $action->row->task_name));
            if ($action->row->total_task_num > 0) $action->append(new TaskAllocationTool($action->row->id, $taskUser));
            if ($action->row->total_task_num === 0) $action->append(new TaskImportTool($action->row->id));
//            if (!empty($taskUser)) $action->append(new TaskProgressTool($action->row->id, $action->row->task_name, $taskUser));

            $action->append(new TaskDeleteTool($action->row->id));
        });

//            $grid->disableActions(false);
        $grid->disableCreateButton();
        $grid->tools(function (Tools $tools) {
            if (in_array(Auth::user()->roles->first()->slug, ['Administrator', 'boss', 'finance'])) $tools->append(new TaskCreateTool);
        });
        return $grid;
    }

    public function form()
    {
        $form = new Form(new TaskModel);
        $form->text('id', 'id');
        return $form;
    }
}
